<?php

namespace App\Logics\Admin\Ucenter;

use App\Commons\Domain\Module\UserCenter\Contract\AccountService;
use Hash;

/**
 *
 * @authors generator
 * @date    2020-11-06 18:40:47
 */
class UserLogic
{
    public function adminLogin(array $param, string $ip)
    {
        if (!$token = auth('admin')->attempt($param)) {
            throw_e(0xf00142);
        }

        app(AccountService::class)->adminLogin(auth('admin')->user(), $ip);

        return compact('token');
    }

    public function adminInfo()
    {
        return app(AccountService::class)->adminInfo(auth()->user());
    }

    public function updatePwd(array $param)
    {
        $user = auth()->user();

        throw_on(!Hash::check($param['password'], $user->account->password), 0xf00172);
        throw_on($param['new_pass'] !== $param['confirm_pass'], 0xf00132);

        return app(AccountService::class)->updatePassword($user, $param['new_pass']);
    }

    public function logout()
    {
        $token = auth()->getToken()->get();
        app(AccountService::class)->userLogout($token);
        auth()->invalidate($token);
    }
}
